package com.inspur.cloud.security;

import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
public class Base64PasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence charSequence) {
        try {
            return Base64.encodeBase64String(charSequence.toString().getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        try {
            if(Base64.encodeBase64String(charSequence.toString().getBytes("utf-8")).equals(s)){
                return true;
            }else {
                return false;
            }
        } catch (UnsupportedEncodingException e) {
            return false;
        }
    }
}
